package com.ewanghuiju.app.ui.home.fragment;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.androidkun.xtablayout.XTabLayout;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseFragment;
import com.ewanghuiju.app.base.contract.home.HomeContract;
import com.ewanghuiju.app.presenter.home.HomePresenter;
import com.ewanghuiju.app.test.bean.TestBean;
import com.ewanghuiju.app.ui.home.activity.ScanActivity;
import com.ewanghuiju.app.ui.home.activity.SearchShopActivity;
import com.ewanghuiju.app.ui.home.adapter.HomePagerAdapter;
import com.ewanghuiju.app.ui.message.activity.MessageCenterActivity;
import com.ewanghuiju.app.util.PermissionUtil;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.ToastUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.ArcView;
import com.ewanghuiju.app.widget.popupwindow.HomemorePopup;
import com.google.android.material.tabs.TabLayout;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;
import razerdp.util.SimpleAnimationUtils;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/16
 */
public class HomeFragment extends BaseFragment<HomePresenter> implements HomeContract.View {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tab_home)
    XTabLayout mTabLayout;
    @BindView(R.id.vp_home)
    ViewPager mViewPager;
    @BindView(R.id.ll_home_menu)
    LinearLayout llHomeMenu;

    public static String[] typeStr = {"首页", "猜你喜欢", "拼团", "e网商城", "美妆", "护肤品", "女装", "女装", "女装", "全部"};
    List<Fragment> fragments = new ArrayList<>();
    @BindView(R.id.tab_layout)
    LinearLayout tabLayout;
    @BindView(R.id.arcView)
    ArcView arcView;

    public static HomeFragment newInstance() {
        Bundle bundle = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initEventAndData() {
        mTabLayout.removeAllTabs();
        fragments.clear();

        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
//        for (int i = 0; i < typeStr.length; i++) {
//            if (i == 0) {
//                ChoicestFragment fragment = new ChoicestFragment();
//                Bundle bundle = new Bundle();
//                bundle.putInt(Constants.IT_HOME_TYPE, i);
//                fragment.setArguments(bundle);
//                mTabLayout.addTab(mTabLayout.newTab());
//                fragments.add(fragment);
//            } else {
//                OtherFragment otherFragment = new OtherFragment();
//                Bundle otherBundle = new Bundle();
//                otherBundle.putInt(Constants.IT_HOME_TYPE, i);
//                otherFragment.setArguments(otherBundle);
//                mTabLayout.addTab(mTabLayout.newTab());
//                fragments.add(otherFragment);
//            }
//        }

        ChoicestFragment fragment = new ChoicestFragment();
        fragments.add(fragment);

        HomePagerAdapter mAdapter = new HomePagerAdapter(getChildFragmentManager(), 0, fragments);
        mViewPager.setAdapter(mAdapter);
//        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void initImmersionBar() {
//        super.initImmersionBar();
//        ImmersionBar.with(this).statusBarColorTransformEnable(false)
//                .keyboardEnable(false)
//                .init();
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void showContent(String list) {

    }

    @Override
    public void showMoreContent(String list) {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == Constants.SCAN_REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    Toast.makeText(context, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(context, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    HomemorePopup popupWindow;

    @OnClick({R.id.iv_scan, R.id.go_search, R.id.ll_home_menu, R.id.iv_message})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.iv_scan:
                PermissionUtil.requestPermissionForFragment(this, new PermissionUtil.IPermissionListener() {
                    @Override
                    public void permissionGranted() {
                        Intent intentScan = new Intent(mActivity, ScanActivity.class);
                        startActivityForResult(intentScan, Constants.SCAN_REQUEST_CODE);
                    }

                    @Override
                    public void permissionDenied() {
                        ToastUtil.show("权限被拒绝");
                    }
                }, "权限被拒绝,请设置应用权限", Manifest.permission.CAMERA);
                break;
            case R.id.go_search:
                //去搜索页面
                new StartActivityUtil(context, SearchShopActivity.class).startActivity(true);
                break;
            case R.id.ll_home_menu:
                //全部分类
                if (popupWindow == null) {
                    popupWindow = new HomemorePopup(context);
                }
//                popupWindow.setBlurOption(new PopupBlurOption()
//                    .setBlurView(mViewPager));
                popupWindow.setBlurBackgroundEnable(false);
                popupWindow.setAlignBackground(true);
                popupWindow.setAlignBackgroundGravity(Gravity.TOP);
                Animation showAnimation = SimpleAnimationUtils.getDefaultScaleAnimation(true);
                Animation dismissAnimation = SimpleAnimationUtils.getDefaultScaleAnimation(false);
                popupWindow.setWidth(popupWindow.getScreenWidth());
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                float fromX = 0;
                float fromY = -1f;
                float toX = 0;
                float toY = 0;
                if (fromX != 0 || fromY != 0) {
                    showAnimation = createTranslateAnimation(fromX, toX, fromY, toY);
                    dismissAnimation = createTranslateAnimation(toX, fromX, toY, fromY);
                }
//                popupWindow.setAutoLocatePopup(false);
                popupWindow.setShowAnimation(showAnimation);
                popupWindow.setDismissAnimation(dismissAnimation);
                popupWindow.setRecycleView(TestBean.getPageGridBean(0));
                popupWindow.showPopupWindow(tabLayout);
                break;
            case R.id.iv_message:
                //消息
                new StartActivityUtil(context, MessageCenterActivity.class).startActivity(true);
                slideRightIn();
                break;
            default:
                break;
        }
    }

}
